﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace FySystem.FyAppConfig
{
    internal class FyConfigs
    {
        private string _realtiveConfigFilename;
        public Dictionary<string, string> Configs = new Dictionary<string, string>();

        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="realtiveConfigFilename"></param>
        internal FyConfigs(string realtiveConfigFilename)
        {
            this._realtiveConfigFilename = realtiveConfigFilename;
            ReadConfigs();
        }

        /// <summary>
        /// 读取配置
        /// </summary>
        internal void ReadConfigs()
        {
            try
            {
                string fullName = AppDomain.CurrentDomain.BaseDirectory + _realtiveConfigFilename;
                using (FileStream fs = new FileStream(fullName, FileMode.OpenOrCreate, FileAccess.Read))
                {
                    using (StreamReader sr = new StreamReader(fs))
                    {
                        while (sr.Peek() != -1)
                        {
                            string line = sr.ReadLine();
                            line = line.Trim();
                            if (string.IsNullOrEmpty(line))
                                continue;
                            if (line[0] == '#')
                                continue;
                            if (line.Contains('=') == false)
                                continue;

                            int index = line.IndexOf('=');
                            string command = line.Substring(0, index).Trim();

                            if (index >= line.Length - 1)
                            {
                                Configs.Add(command, "");
                            }
                            else
                            {
                                string value = line.Substring(index + 1).Trim();
                                Configs.Add(command, value);
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
